package com.elinkway.infinitemovies.ui.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.elinkway.infinitemovies.adapter.LiveVideoListAdapter;
import com.elinkway.infinitemovies.async.RequestLiveChannelTask;
import com.elinkway.infinitemovies.async.RequestResultListener;
import com.elinkway.infinitemovies.bean.LiveClassifyInfos;
import com.elinkway.infinitemovies.bean.LiveTvStationDetail;
import com.elinkway.infinitemovies.bean.PlayData;
import com.elinkway.infinitemovies.listener.MyOnRefreshListner;
import com.elinkway.infinitemovies.ui.activity.play.PlayActivityLive;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.elinkway.infinitemovies.utils.UIs;
import com.elinkway.infinitemovies.utils.Utils;
import com.elinkway.infinitemovies.view.PublicLoadLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.le123.ysdq.R;

/**
 * 直播信息Frament依附于LiveActivity用于显示LiveActivity页面下方tab的内容
 * @author zhangshuo
 */
public class LiveInfoFragment extends Fragment implements RequestResultListener<LiveClassifyInfos> {

    private static final String TAG = "LiveInfoFragment";
    public static final String ARGUMENT_IDENTIFIER = "identifier";
    public static final String ARGUMENT_CATEGORY_CHINESE_NAME = "chinesename";

    private boolean hasMore = true;

    private PublicLoadLayout mRoot;

    private PullToRefreshListView mListView;

    private LiveVideoListAdapter mLiveListAdapter;
    private LiveClassifyInfos mCategoryInfo = new LiveClassifyInfos();

    private RequestLiveChannelTask mRequestTask;

    private int mPageNum = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRoot = UIs.createPage(getActivity(), R.layout.fragment_liveinfo);
        mRoot.loading(false);
        mCategoryInfo.setIdentifier(getArguments().getString(ARGUMENT_IDENTIFIER));
        mCategoryInfo.setCheineseName(getArguments().getString(ARGUMENT_CATEGORY_CHINESE_NAME));
        mRequestTask = new RequestLiveChannelTask(getActivity());
        mRequestTask.setParamChannel(mCategoryInfo.getIdentifier());
        mRequestTask.setParamPage(mPageNum);
        mRequestTask.setParamPageSize(MoviesConstant.LIVE_CHANNEL_PAGE_SIZE);
        mRequestTask.setmListener(this);
        mRequestTask.start();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView();
        return mRoot;
    }

    private void initView() {
        mListView = (PullToRefreshListView) mRoot.findViewById(R.id.liveinfo_list);
        resetHasMore();
        mListView.setAdapter(mLiveListAdapter);
        mListView.setOnItemClickListener(new LiveOnItemClickListener());
        mListView.setOnRefreshListener(new LiveOnRefreshListener());
    }

    private class LiveOnItemClickListener implements OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int dataIndex = position - mListView.getRefreshableView().getHeaderViewsCount();
            LiveTvStationDetail statiionDetail = mCategoryInfo.getmChannels().get(dataIndex);
            PlayData playData = new PlayData();
            playData.setmLiveId(statiionDetail.getId()); //设置直播唯一标示
            playData.setLiveStreams(statiionDetail.getStreams());
            playData.setmViewName(statiionDetail.getName());
            Bundle bundle = new Bundle();
            bundle.putSerializable(Utils.PLAY_DATA, playData);
            Intent intent = new Intent();
            intent.setClass(getActivity(), PlayActivityLive.class);
            intent.putExtras(bundle);
            getActivity().startActivity(intent);
        }
    }

    private class LiveOnRefreshListener extends MyOnRefreshListner {

        @Override
        protected void onLoadMoreData() {
            mRequestTask.setParamPage(mPageNum);
            mRequestTask.setParamPageSize(MoviesConstant.LIVE_CHANNEL_PAGE_SIZE);
            mRequestTask.start();
        }

        @Override
        protected void onRefreshData() {
            resetHasMore();
            mPageNum = 1;
            mRequestTask.setParamPage(mPageNum);
            mRequestTask.setParamPageSize(MoviesConstant.LIVE_CHANNEL_PAGE_SIZE);
            mRequestTask.start();
        }

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mListView.onRefreshComplete();
            super.handleMessage(msg);
        }
    };

    private void setNoMore() {
        hasMore = false;
        mListView.setLoadingDrawable(getResources().getDrawable(R.drawable.transparent), Mode.PULL_UP_TO_REFRESH);
        mListView.setRefreshingLabel(getString(R.string.havenomore), Mode.PULL_UP_TO_REFRESH);
        mListView.setReleaseLabel(getString(R.string.havenomore), Mode.PULL_UP_TO_REFRESH);
        mListView.setPullLabel(getString(R.string.havenomore), Mode.PULL_UP_TO_REFRESH);
    }

    private void resetHasMore() {
        hasMore = true;
        mListView.setLoadingDrawable(getResources().getDrawable(R.drawable.default_ptr_rotate), Mode.PULL_UP_TO_REFRESH);
        mListView.setRefreshingLabel(getString(R.string.loading), Mode.PULL_UP_TO_REFRESH);
        mListView.setReleaseLabel(getString(R.string.release_loadmore), Mode.PULL_UP_TO_REFRESH);
        mListView.setPullLabel(getString(R.string.pull_loadmore), Mode.PULL_UP_TO_REFRESH);
    }

    @Override
    public boolean onRequestFailed() {
        mListView.onRefreshComplete();
        return false;
    }

    @Override
    public void onRequestSuccess(int updateId, LiveClassifyInfos result) {
        if (mPageNum == 1) {
            mCategoryInfo = result;
            mLiveListAdapter = new LiveVideoListAdapter(getActivity(), result);
            mListView.setAdapter(mLiveListAdapter);
            mRoot.finish();
        } else {
            mCategoryInfo.getmChannels().addAll(result.getmChannels());
            mLiveListAdapter.notifyDataSetChanged();
        }
        if (result.getmChannels().size() == 0) {
            setNoMore();
        }
        mPageNum++;
        mListView.onRefreshComplete();
    }

    @Override
    public void onPreRequest() {

    }
}
